using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class MessageDisplayController : MonoBehaviour
{
	public UISprite allertSprite;

	public GameObject display;

	public GameObject loadingAllert;

	public GameObject noMessagesAllert;

	public GameObject messageButtonPrefab;

	public UITable table;

	public UIScrollView view;

	private List<GGMessageButton> buttons = new List<GGMessageButton>();

	private bool isLoading;

	private bool hasBeenAligned;

	private int fetchTexturesRequestTicket = -1;

	private int outstandingTextureRequests;

	private void OnEnable()
	{
		BeginLoading();
	}

	private void OnDisable()
	{
		HideButtons(0);
		if (fetchTexturesRequestTicket >= 0 && BehaviourSingleton<GGRequestLimitter>.instance != null)
		{
			BehaviourSingleton<GGRequestLimitter>.instance.StopRequestsWithGroup(fetchTexturesRequestTicket);
		}
	}

	public void BeginLoading()
	{
		if (!isLoading)
		{
			isLoading = true;
			UpdateUI();
			BehaviourSingletonInit<GGServerMessages>.instance.GetMessages(MessageRequestComplete);
		}
	}

	public void MessageRequestComplete(GGServerRequestsBackend.ServerRequest request)
	{
		isLoading = false;
		if (request.status != 0)
		{
			if (this != null)
			{
				UpdateUI();
			}
		}
		else
		{
			ServerMessages response = request.GetResponse<ServerMessages>();
			GenerateMessageButtons(response);
			UpdateUI();
		}
	}

	private void UpdateUI()
	{
		loadingAllert.SetActive(isLoading);
		bool flag = HasActiveMessages();
		allertSprite.gameObject.SetActive(flag);
		noMessagesAllert.SetActive(!isLoading && !flag);
	}

	private bool HasActiveMessages()
	{
		foreach (GGMessageButton button in buttons)
		{
			if (button.gameObject.activeSelf)
			{
				return true;
			}
		}
		return false;
	}

	private void GenerateMessageButtons(ServerMessages messages)
	{
		InitialiseButtons(messages);
		HideButtons(messages.messages.Count);
		table.Reposition();
		hasBeenAligned = false;
	}

	private void InitialiseButtons(ServerMessages messages)
	{
		fetchTexturesRequestTicket = BehaviourSingleton<GGRequestLimitter>.instance.GetGroupId();
		for (int i = 0; i < messages.messages.Count; i++)
		{
			ServerMessages.GGServerMessage message = messages.messages[i];
			if (i >= buttons.Count)
			{
				AddNewMessageButton(GGFormat.IndexName(i));
			}
			buttons[i].gameObject.SetActive(true);
			buttons[i].Init(message, fetchTexturesRequestTicket, this);
			outstandingTextureRequests++;
		}
	}

	private void AddNewMessageButton(string name)
	{
		GameObject gameObject = NGUITools.AddChild(table.gameObject, messageButtonPrefab);
		gameObject.name = name;
		GGMessageButton component = gameObject.GetComponent<GGMessageButton>();
		buttons.Add(component);
	}

	public void OnClickShowMessagesButton()
	{
		if (display.activeSelf)
		{
			HideDisplay();
		}
		else
		{
			ShowDisplay();
		}
	}

	private void ShowDisplay()
	{
		if (!HasActiveMessages())
		{
			BeginLoading();
		}
		display.SetActive(true);
	}

	private void HideDisplay()
	{
		display.SetActive(false);
		HideButtons(0);
	}

	private void HideButtons(int startingIndex)
	{
		for (int i = startingIndex; i < buttons.Count; i++)
		{
			buttons[i].gameObject.SetActive(false);
		}
	}

	private void Update()
	{
		if (!hasBeenAligned && !isLoading)
		{
			table.Reposition();
			if (buttons.Count > 0)
			{
				UITools.AlignToTopOnScroll(buttons[0].gameObject.GetComponent<UIWidget>(), view, 0f);
			}
			hasBeenAligned = true;
		}
	}

	public void SortTable()
	{
		hasBeenAligned = false;
		UpdateUI();
	}

	public void AcceptAllGifts()
	{
		string allGiftIds = GetAllGiftIds();
		SendAnyAcceptedIds(allGiftIds);
	}

	private string GetAllGiftIds()
	{
		string text = string.Empty;
		for (int i = 0; i < buttons.Count; i++)
		{
			if (buttons[i].CanAccept())
			{
				text += ((i <= 0) ? buttons[i].GetIdAndBeginAccept() : (", " + buttons[i].GetIdAndBeginAccept()));
			}
		}
		return text;
	}

	private void SendAnyAcceptedIds(string ids)
	{
		if (ids != string.Empty)
		{
			BehaviourSingletonInit<GGServerMessages>.instance.MarkMessageAsRead(ids, OnAcceptAllFinished);
		}
	}

	public void OnAcceptAllFinished(GGServerRequestsBackend.ServerRequest request)
	{
		if (request.status != 0)
		{
			DisplayErrorWithServer();
			return;
		}
		foreach (GGMessageButton button in buttons)
		{
			if (button.isGroupAccepting)
			{
				button.ApplyMessageOnSuccess(request);
			}
		}
	}

	private void DisplayErrorWithServer()
	{
		TableTennisDialog.instance.ShowOk("Server currently unavailable", "Server currently unavailable!\n Please try again later", "Ok", delegate
		{
			foreach (GGMessageButton button in buttons)
			{
				button.loadingDisplay.SetActive(false);
			}
			NavigationManager.instance.Pop();
		});
	}

	public void OnTextureArrived()
	{
		outstandingTextureRequests--;
		if (outstandingTextureRequests <= 0)
		{
			outstandingTextureRequests = 0;
			fetchTexturesRequestTicket = -1;
		}
	}
}
